package com.arvato.wxwork.controller.front.dictionary;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.common.ftp.FtpTool;
import com.arvato.wxwork.bean.bean_do.dictionary.WxworkPromotionDo;
import com.arvato.wxwork.bean.bean_vo.dictionary.WxworkPromotionVo;
import com.arvato.wxwork.service.dictionary.WxworkPromotionService;

/**
 * 企业微信员工编辑商场活动新增编辑
 * 活动列表查看Controller
 * @author ZHAN655
 */
@RestController
@RequestMapping("/wxwork/promotion")
public class WxworkPromotionController{
	
	@Autowired
	private WxworkPromotionService wxworkPromotionService;
	@Autowired
	private FtpTool ftpTool;
	private Logger logger = LoggerFactory.getLogger(WxworkPromotionController.class);
	
	/**
	 * 获取商场活动列表
	 * @param req
	 * @param param 查询参数 staffId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/list")
	public Result listPromotionByQuery(HttpServletRequest req, @RequestParam Map<String, Object> param) {
		Query query = new Query(param);
		PageData pageData = wxworkPromotionService.listPromotionByQuery(query, query.getPageNum(), query.getPageSize());
		return Result.success(pageData);
	}
	
	
	/**
	 * 根据ID查询一个商场活动
	 * @param req
	 * @param promotionId 活动ID
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/get")
	public Result getPromotionById(HttpServletRequest req, String promotionId) {
		WxworkPromotionVo promotionVo = wxworkPromotionService.getPromotionById(promotionId);
		return Result.success(promotionVo);
	}
	
	/**
	 * 新增一个活动推广
	 * @param req
	 * @param promotionDo
	 * @param file
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/insert")
	public Result get(HttpServletRequest req, WxworkPromotionDo promotionDo, @RequestParam("file") MultipartFile file) {
		logger.info("新增一个活动推广:" + promotionDo.toString());
		// 将文件存储在远程服务器上
		try {
			InputStream inputStream = file.getInputStream();
		} catch (IOException e1) {
			e1.printStackTrace();
			
		}
		String fileName = ftpTool.uploadFile(file);
		if(fileName == null) {
			logger.info("图片上传失败");
			return Result.fail("图片上传失败。");
		}
		promotionDo.setImgUrl(fileName);
		promotionDo.setCreateTime(new Date());
		promotionDo.setType(0);
		try {
			int row = wxworkPromotionService.insert(promotionDo);
			if(row == 1){
				return Result.success("活动推广上传成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail("上传失败。", e);
		}
		return Result.fail("上传失败。");
	}
	/**
	 * 修改一个活动推广(更改图片) 因为坑爹的小程序接口 所以将此接口分成两个接口
	 * @param promotionDo
	 * @param file
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update")
	public Result update(WxworkPromotionDo promotionDo, @RequestParam("file") MultipartFile file){
		// 文件存储在远程服务器上
		if(file != null) {
			String fileName = ftpTool.uploadFile(file);
			if(fileName != null) {
				promotionDo.setImgUrl(fileName);
			}
		}
		promotionDo.setUpdateTime(new Date());
		try {
			int row = wxworkPromotionService.update(promotionDo);
			if(row == 1){
				return Result.success("修改成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Result.fail("修改失败。");
	}
	/**
	 * 修改一个活动推广(不更改图片)
	 * @param promotionDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update_notfile")
	public Result updateNotFile(WxworkPromotionDo promotionDo){
		promotionDo.setUpdateTime(new Date());
		try {
			int row = wxworkPromotionService.update(promotionDo);
			if(row == 1){
				return Result.success("修改成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Result.fail("修改失败。");
	}
	/**
	 * 删除一个活动推广
	 * @param promotionId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
	public Result delete(String promotionId){
		try {
			int row = wxworkPromotionService.delete(promotionId);
			if(row == 1){
				return Result.success("删除成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Result.fail("删除失败");
	}
}
